class GameEventManager {
	private static _instance:GameEventManager;
	private _dictionary: any[];
	public constructor() {
		this._dictionary = [];
	}
	public static getInstance():GameEventManager{
		if(!this._instance){
			this._instance=new GameEventManager();
		}
		return this._instance;
	}
	/**添加监听 */
	public addEventListener(type: string, func: Function, thisObj: any) {
		let len = this._dictionary.length;
		/**检测是否已经存在 */
		for (let i = 0; i < len; i++) {
			let dt = this._dictionary[i];
			if (dt["type"] == type && dt["func"] == func && dt["thisObj"] == thisObj) {
				return;
			}
		}
		/**保存 */
		let dt = { "type": type, "func": func, "thisObj": thisObj };
		this._dictionary.push(dt);
	}
	/**移除监听 */
	public removeEventListener(type: string, func: Function, thisObj: any) {
		let len = this._dictionary.length;
		/**检测是否已经存在 */
		for (let i = 0; i < this._dictionary.length; i++) {
			let dt = this._dictionary[i];
			if (dt["type"] == type && dt["func"] == func && dt["thisObj"] == thisObj) {
				dt = null;
				this._dictionary.splice(i, 1);
				return;
			}
		}
	}
	/**派发事件 */
	public dispatchEvent(type:string,data:any=null) {
		//遍历数组，进行响应
		let len = this._dictionary.length;
		// let type=e.type;
		// let data=e.data;
		for (let i = 0; i < this._dictionary.length; i++) {
			let dt = this._dictionary[i];
			let dtype=dt["type"];
			if (dtype == type) {
				let func: Function = dt["func"];
				let thisObj = dt["thisObj"];
				func.call(thisObj, data);
			}
		}
	}
	/**清空 */
	public clear() {
		this._dictionary.splice(0, this._dictionary.length);
	}
	/**按type清空 */
	public clearByType(type: string) {
		for (let i = 0; i < this._dictionary.length; i++) {
			let dt = this._dictionary[i];
			if (dt["type"] == type) {
				dt = null;
				this._dictionary.splice(i, 1);
				i -= 1;
			}
		}
	}
}